package com.zqs.takeredpackage.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.zqs.takeredpackage.entity.RedPackage;
import com.zqs.takeredpackage.service.RedPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/red-package")
public class RedPackageController {

    @Autowired
    private RedPackageService redPackageService;

    /**
     * 查询大厅
     */
    @RequestMapping("/list")
    @SaCheckLogin
    public SaResult list() {
        List<RedPackage> redPackages = redPackageService.list();
        return SaResult.data(redPackages);
    }

    /**
     * 查询自己全部红包
     */
    @RequestMapping("/my-list")
    @SaCheckLogin
    public SaResult myList() {
        ArrayList<RedPackage> redPackages = redPackageService.myList();
        return SaResult.data(redPackages);
    }

    /**
     * 获取红包
     */
    @RequestMapping("/get")
    @SaCheckLogin
    public SaResult get(@RequestParam String redId) {
        RedPackage redPackage = redPackageService.get(redId);
        return SaResult.data(redPackage);
    }

    /**
     * 设置红包数
     */
    @RequestMapping("/set")
    @SaCheckLogin
    public synchronized SaResult set(@RequestParam int size, @RequestParam BigDecimal money) {
        return redPackageService.set(size, money);
    }

    /**
     * 抢红包
     */
    @RequestMapping("/take")
    @SaCheckLogin
    public synchronized SaResult take(@RequestParam String redId) {
        return redPackageService.take(redId);
    }

    /**
     * 获取抢到红包用户
     */
    @RequestMapping("/takers")
    @SaCheckLogin
    public SaResult takers(@RequestParam String redId) {
        return redPackageService.takers(redId);
    }

    /**
     * 撤销红包
     */
    @RequestMapping("/cancel")
    @SaCheckLogin
    public synchronized SaResult cancel(@RequestParam String redId) {
        redPackageService.cancel(redId);
        return SaResult.ok("撤销成功！");
    }

    /**
     * 用户余额排名
     */
    @RequestMapping("/rank")
    public SaResult rank() {
        return SaResult.data(redPackageService.rank());
    }
}
